package com.bw.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bw.common.resp.ResponseResult;
import com.bw.entity.TbResource;

import com.bw.entity.TbUser;
import com.bw.entity.vo.Group;
import com.bw.service.ITbResourceService;
import com.bw.service.ITbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/resource")
@Api(tags = "数据资产包管理")
public class TbResourceController {

    @Autowired
    private ITbResourceService resourceService;

    @Autowired
    private ITbUserService userService;

    /**
     * TODO:数据包列表
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @param id:
     * @return: com.bw.common.resp.ResponseResult
     **/
    @PostMapping("/list/{id}")
    @ApiOperation(value = "资产包列表", notes = "资产包列表")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", required = true, example = "0"),
    })
    public ResponseResult list(@PathVariable("id") Integer id) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            Map map = new HashMap();
            map.put("id",id);
            List<TbResource> list = resourceService.findlist(map);
            result.setResult(list);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询失败" + e.toString());
        }
        return result;

    }


    /**
     * TODO:根据状态查看数据
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @PostMapping("/listStatus3")
    @ApiOperation(value = "根据状态查看", notes = "根据状态查看")
    public ResponseResult listStatus3() {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("status",3);
            List<TbResource> list = resourceService.list(queryWrapper);
            result.setResult(list);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询失败" + e.toString());
        }
        return result;

    }


    /**
     * TODO:根据角色查看数据
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @PostMapping("/listStatus/{id}")
    @ApiOperation(value = "根据角色查看数据", notes = "根据角色查看数据")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", required = true, example = "0"),
    })
    public ResponseResult listStatus(@PathVariable("id") Long id) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            Map map = new HashMap();
            map.put("id",id);
            List<Group> list = resourceService.findListStatus(map);
            result.setResult(list);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("查询失败" + e.toString());
        }
        return result;

    }


    /**
     * TODO:新建资产包
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新建资产包", notes = "新建资产包")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "resource", value = "resource", dataType = "TbResource", required = true, example = "0"),
    })
    public ResponseResult save(@RequestBody TbResource resource) {
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            resourceService.saveOrUpdate(resource);
            result.setMessage("新建成功");
            result.setSuccess(true);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("新建失败" + e.toString());
        }
        return result;

    }

    /**
     * TODO:上传文件
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @PostMapping("upload")
    @ApiOperation(value = "上传文件", notes = "上传文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "file", value = "file", dataType = "MultipartFile", required = true, example = "0"),
    })
    public ResponseResult upload(MultipartFile file){
        ResponseResult result = ResponseResult.SUCCESS();
        try{
            String ext = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."),file.getOriginalFilename().length());
            String dirPath="D:\\workSpace\\fileDB\\";
            String path= UUID.randomUUID()+ext;
            File mFile = new File(dirPath+path);
            file.transferTo(mFile);
            Map map = new HashMap();
            map.put("path",path);
            result.setResult(map);
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("上传失败"+e.toString());
        }
        return result;
    }

    /**
     * TODO:下载文件
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @GetMapping("download/{path}")
    @ApiOperation(value = "下载文件", notes = "下载文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "path", value = "path", dataType = "String", required = true, example = "0"),
    })
    public void download(@PathVariable("path") String path, HttpServletResponse response){
        ResponseResult result = ResponseResult.SUCCESS();
        if(path==null){
            return;
        }
        try{
            InputStream inputStream = new FileInputStream("D:\\workSpace\\fileDB\\"+path);
            byte[] b = new byte[1024];
            int len;

            while((len = inputStream.read(b))>0){
                response.getOutputStream().write(b,0,len);
            }
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * TODO:删除数据资源包
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @PostMapping("/deleteResource")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "tbResource", value = "tbResource", dataType = "TbResource", required = true, example = "0"),
    })
    @ApiOperation(value = "删除数据资源包", notes = "删除数据资源包")
    public ResponseResult deleteCar(@RequestBody TbResource tbResource){
        ResponseResult result = ResponseResult.SUCCESS();
        try{
            tbResource.setStatus(5);
            resourceService.saveOrUpdate(tbResource);
            result.setSuccess(true);
            result.setMessage("删除成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("删除失败"+e.toString());
        }
        return result;
    }

    /**
     * TODO:推送资源包
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @PostMapping("/push/{id}")
    @ApiOperation(value = "推送资源包", notes = "推送资源包")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", required = true, example = "0"),
    })
    public ResponseResult push(@PathVariable("id") Long id){
        ResponseResult result = ResponseResult.SUCCESS();
        try{
            TbResource byId = resourceService.getById(id);
            byId.setStatus(2);

            byId.setTid(id);
            resourceService.updateById(byId);
            result.setSuccess(true);
            result.setMessage("成功");
        }catch (Exception e){
            result.setSuccess(true);
            result.setMessage("成功");
        }
        return result;
    }

    /**
     * TODO:根据id获取资源包
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @GetMapping("/get/{id}")
    @ApiOperation(value = "根据id获取资源包", notes = "根据id获取资源包")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", required = true, example = "0"),


    })
    public ResponseResult get(@PathVariable("id") Integer id){
        ResponseResult result = ResponseResult.SUCCESS();
        try{
            TbResource byId = resourceService.getById(id);
            result.setResult(byId);
            result.setSuccess(true);
        }catch (Exception e){
            result.setSuccess(false);
        }
        return result;
    }



    /**
     * TODO:发布资源包
     * @Author: 范欣宜
     * @Date: 2020/10/12
     * @return: com.bw.common.resp.ResponseResult
     **/
    @PostMapping("/publish/{id}")
    @ApiOperation(value = "发布资源包", notes = "发布资源包")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", required = true, example = "0"),


    })
    public ResponseResult publish(@PathVariable("id") Integer id){
        ResponseResult result = ResponseResult.SUCCESS();
        try{
            TbResource byId = resourceService.getById(id);
            byId.setStatus(3);
            resourceService.updateById(byId);
            result.setSuccess(true);
            result.setMessage("成功");
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("失败"+e.toString());
        }
        return result;
    }




}
